const bodyParser = require('body-parser')
// const jwt = require('jsonwebtoken')
const user = [
  { username: 'admin', password: 'admin', role: 'admin', nickname: '管理员', avatar: 'https://cn.vuejs.org/images/logo.svg' },
  { username: 'editor', password: 'editor', role: 'editor', nickname: '普通用户', avatar: 'https://cn.vuejs.org/images/logo.svg' }
]
const { housing, broker, information, order, live, audit } = require('./src/utils/mock')

module.exports = {
  lintOnSave: false,
  devServer: {
    before: app => {
      app.use(bodyParser.json())
      app.post('/api/login', (req, res) => {
        const { username, password } = req.body
        const flag = user.find(item => item.username == username && item.password == password)
        if (flag) {
          res.send({
            code: 1,
            data: {
              nickName: flag.nickname,
              role: flag.role,
              avatar: flag.avatar,
              token: 'sduhiuashdiuciusabiudhuiaweiuwcnjni'
            }
          })
        } else {
          res.send({
            code: 0,
            data: null
          })
        }
      })

      // 房源管理
      app.get('/api/gethousing/:type', (req, res) => {
        const { type } = req.params //  sellTower  tenement
        const { keyWord = '', pageindex = 1, pagesize = 5 } = req.query
        if (housing.hasOwnProperty(type)) {
          let housingList = housing[type]
          if (keyWord)housingList = housingList.filter(item => item.name.includes(keyWord))
          if (housingList.length > pagesize) { // 需要分页
            res.send({
              meta: { status: 1, msg: '获取数据成功' },
              data: {
                pageindex,
                pagesize,
                total: housingList.length,
                list: housingList.slice((pageindex - 1) * pagesize, pageindex * pagesize)
              }
            })
          } else { // 不需要分页
            res.send({
              meta: { status: 1, msg: '获取数据成功' },
              data: {
                pageindex: 1,
                pagesize,
                total: housingList.length,
                list: housingList
              }
            })
          }
        } else {
          res.send({
            meta: { status: 0, msg: '参数有误' },
            data: null
          })
        }
      })
      app.post('/api/housingEdit/:type', (req, res) => {
        const { type } = req.params
        const { housingForm = {}, isSwitch, id } = req.body
        const flag = JSON.stringify(housingForm) == '{}'
        if (flag && id) {
          // 切换switch
          const findData = housing[type].find(item => item.id == id)
          findData.status = isSwitch
          res.send({
            meta: { status: 1, msg: '状态切换成功' },
            data: null
          })
          return
        }
        const result = editDataFun(housing[type], housingForm)
        res.send(result)
      })
      app.delete('/api/housingDel/:type', (req, res) => {
        const { id } = req.body
        const { type } = req.params
        const result = delDataFun(housing[type], id)
        res.send(result)
      })
      app.put('/api/housingAdd/:type', (req, res) => {
        const { housingForm } = req.body
        const { type } = req.params
        const result = addDataFun(housing[type], housingForm) || { meta: { status: 0, msg: '未知错误' } }
        res.send(result)
      })

      // 经纪人管理
      app.get('/api/getbroker', (req, res) => {
        const { keyWord = '', pageindex = 1, pagesize = 5, status = '' } = req.query
        console.log(status)
        let brokerList = [...broker]
        brokerList = brokerList.filter(item => item.name.includes(keyWord) && item.status.includes(status))
        if (brokerList.length > pagesize) { // 需要分页
          res.send({
            meta: { status: 1, msg: '获取数据成功' },
            data: {
              pageindex,
              pagesize,
              total: brokerList.length,
              list: brokerList.slice((pageindex - 1) * pagesize, pageindex * pagesize)
            }
          })
        } else {
          res.send({
            meta: { status: 1, msg: '获取数据成功' },
            data: {
              pageindex,
              pagesize,
              total: brokerList.length,
              list: brokerList
            }
          })
        }
      })
      app.put('/api/addBroker', (req, res) => {
        const { brokerForm } = req.body
        const result = addDataFun(broker, brokerForm) || { meta: { status: 0, msg: '未知错误' } }
        res.send(result)
      })
      app.delete('/api/delBroker', (req, res) => {
        const { id } = req.body
        const result = delDataFun(broker, id)
        res.send(result)
      })
      app.post('/api/editBroker', (req, res) => {
        const { id, brokerForm } = req.body
        const result = editDataFun(broker, brokerForm)
        res.send(result)
      })

      // 资讯管理
      app.get('/api/information', (req, res) => {
        const { keyWord = '', pageindex = 1, pagesize = 5 } = req.query
        let informationList = [...information]
        if (keyWord)informationList = informationList.filter(item => item.name.includes(keyWord))
        if (informationList.length > pagesize) { // 需要分页
          res.send({
            meta: { status: 1, msg: '获取数据成功' },
            data: {
              pageindex,
              pagesize,
              total: informationList.length,
              list: informationList.slice((pageindex - 1) * pagesize, pageindex * pagesize)
            }
          })
        }
      })
      app.delete('/api/delInfo', (req, res) => {
        const { id } = req.body
        const result = delDataFun(information, id)
        res.send(result)
      })
      app.post('/api/editInfo', (req, res) => {
        const { infoForm = {}, isSwitch, id } = req.body
        const flag = JSON.stringify(infoForm) == '{}'
        if (flag && id) {
          // 切换switch
          const findData = information.find(item => item.id == id)
          findData.status = isSwitch

          res.send({
            meta: { status: 1, msg: '状态切换成功' },
            data: null
          })
          return
        }
        const result = editDataFun(information, infoForm)
        res.send(result)
      })
      app.put('/api/addInfo', (req, res) => {
        const { infoForm } = req.body
        const result = addDataFun(information, infoForm) || { meta: { status: 0, msg: '未知错误' } }
        res.send(result)
      })

      // 订单管理
      app.get('/api/order', (req, res) => {
        const { keyWord = '', pageindex = 1, pagesize = 5 } = req.query
        let orderList = [...order]
        if (keyWord)orderList = orderList.filter(item => item.name.includes(keyWord))
        if (orderList.length > pagesize) { // 需要分页
          res.send({
            meta: { status: 1, msg: '获取数据成功' },
            data: {
              pageindex,
              pagesize,
              total: orderList.length,
              list: orderList.slice((pageindex - 1) * pagesize, pageindex * pagesize)
            }
          })
        }
      })
      app.delete('/api/order', (req, res) => {
        const { id } = req.body
        const result = delDataFun(order, id)
        res.send(result)
      })

      // 直播管理
      app.get('/api/live', (req, res) => {
        const { keyWord = '', pageindex = 1, pagesize = 5, type = '' } = req.query
        let liveList = [...live]

        if (keyWord)liveList = liveList.filter(item => item.name.includes(keyWord) 
          &&item.name.includes(keyWord)
        )
        if (liveList.length > pagesize) { // 需要分页
          res.send({
            meta: { status: 1, msg: '获取数据成功' },
            data: {
              pageindex,
              pagesize,
              total: liveList.length,
              list: liveList.slice((pageindex - 1) * pagesize, pageindex * pagesize)
            }
          })
        }
      })
      // 审核管理
      app.get('/api/audit/:type', (req, res) => {
        const { type = 'all' } = req.params //  all  checkSuccess checkFailed
        const { keyWord = '', pageindex = 1, pagesize = 5 } = req.query
        let auditList = [...audit]
        const keys = {
          all: '',
          checkSuccess: '审核通过',
          checkFailed: '审核不通过'
        }

        if (!keys.hasOwnProperty(type)) {
          res.send({
            meta: { status: 0, msg: '参数有误' },
            data: null
          })
          return
        }

        auditList = auditList.filter(item => {
          return item.name.includes(keyWord) && item.status.includes(keys[type])
        })
        console.log(auditList.length > pagesize)
        if (auditList.length > pagesize) { // 需要分页
          res.send({
            meta: { status: 1, msg: '获取数据成功' },
            data: {
              pageindex,
              pagesize,
              total: auditList.length,
              list: auditList.slice((pageindex - 1) * pagesize, pageindex * pagesize)
            }
          })
        } else {
          res.send({
            meta: { status: 1, msg: '获取数据成功' },
            data: {
              pageindex,
              pagesize,
              total: auditList.length,
              list: auditList
            }
          })
        }
      })
      app.post('/api/editAudt', (req, res) => {
        const { auditForm = {} } = req.body
        const flag = JSON.stringify(auditForm) == '{}'
        if (flag) {
          res.send({
            meta: { status: 0, msg: '参数有误,编辑失败' },
            data: null
          })
          return
        }
        const result = editDataFun(audit, auditForm)
        res.send(result)
      })
      app.delete('/api/delAudit', (req, res) => {
        const { id } = req.body
        const result = delDataFun(audit, id)
        res.send(result)
      })
      function limit (arr, pageindex, pagesize) {
        const arrList = [...arr]
        if (arrList.length > pagesize) { // 需要分页

        }
        arrList.slice((pageindex - 1) * pagesize, pageindex * pagesize)
        return {
          // list:,
          pageindex,
          pagesize,
          total: cc
        }
      }
      function searchKeyWord (arr, keyWord) {
        let arrList = [...arr]
        if (keyWord)arrList = arrList.filter(item => item.name.includes(keyWord))
        return arrList
      }
      function addDataFun (arr, awaitAddObj) {
        const complate = arr[0]
        const addData = {}
        for (const key in complate) {
          addData[key] = awaitAddObj[key]
        }
        addData.id = Math.random().toString(16).slice(3)
        arr.push(addData)
        return {
          meta: { status: 1, msg: '添加成功' },
          data: null
        }
      }
      function delDataFun (arr, awaitDelId) {
        const delIndex = arr.findIndex(item => item.id == awaitDelId)
        if (delIndex !== -1) {
          arr.splice(delIndex, 1)
          return {
            meta: {
              status: 1,
              msg: '删除成功'
            },
            data: null
          }
        }
        return {
          meta: {
            status: 0,
            msg: '删除失败,参数有误'
          },
          data: null
        }
      }
      function editDataFun (arr, awaitEditObj) {
        const findData = arr.find(item => item.id == awaitEditObj.id)
        if (findData) {
          for (const key in findData) {
            findData[key] = awaitEditObj[key]
          }
          return {
            meta: { status: 1, msg: '编辑成功' },
            data: null
          }
        } else {
          return {
            meta: { status: 0, msg: '参数有误,编辑失败' },
            data: null
          }
        }
      }
    }
  }
}
